﻿using System.Security.Cryptography.Pkcs;
using DFGK.IService.BBS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 工作流
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkflowController : BaseController
    {
        private readonly IWorkflowService _workflowService;
        public WorkflowController(IWebHostEnvironment webEnvironment, IWorkflowService workflowService) : base(webEnvironment)
        {
            _workflowService = workflowService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="busName">业务名称</param>
        /// <param name="status">业务状态</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? busName, string? status, int pageIndex, int pageSize)
        {
            int total = 0;
            var page = _workflowService.GetWorkFlowPageData(busName, status, out total, pageIndex, pageSize);
            return Ok(BaseResult.Result(page, "查询成功！", "200", total));
        }

        /// <summary>
        /// 删除工作流
        /// </summary>
        /// <param name="workFlowId">工作流id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelWorkFlow(long workFlowId)
        {
            var result = _workflowService.DelWorkflow(workFlowId, out string errorMsg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！" + errorMsg, "500"));
        }

        /// <summary>
        /// 新增/修改工作流
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitWorkFlow(WorkFlow model)
        {
            if (model.Id > 0)
            {
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateUserName = base.BaseGetNowUserName();
            }
            bool result = _workflowService.SubmitWorkflow(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// Id查询
        /// </summary>
        /// <param name="workFlowId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWorkFlow(long workFlowId)
        {
            var cobj = _workflowService.GetWorkflow(workFlowId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 获取审批业务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAduitBussinessSelectData()
        {
            var data = typeof(EmAduitBusinessType).EnumToDictionary().Select(dic => new { Id = dic.Key, Name = dic.Value }).ToList();
            return Ok(BaseResult.Result(data
                , "查询成功", "200"));
        }


        /// <summary>
        /// 获取审批条件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAduitConditionSelectData()
        {
            var data = typeof(EmAduitConditionType).EnumToDictionary().Select(dic => new { Id = dic.Key, Name = dic.Value }).ToList();
            return Ok(BaseResult.Result(data
                , "查询成功", "200"));
        }


        /// <summary>
        /// 提交业务审批流
        /// </summary>
        /// <param name="orderValue">1-入库 </param>
        /// <param name="aduitStatus">aduitStatus  0-待提交 1-审批中  2-拒绝 3-同意</param>
        /// <param name="orderId">订单id</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitOrderWorkFlow([FromBody] WorkFlowAduitInputModel workFlowAduitInputModel)
        {
            string errorMsg = "";
            int uid =(int) base.BaseGetNowUserId();
            string uname = base.BaseGetNowUserName();
            bool result = _workflowService.SubmitWorkFlowOrder(workFlowAduitInputModel, uid, uname, out errorMsg);
            if (!result)
            {
                return Ok(BaseResult.Result(null, "操作失败！" + errorMsg, "500", 0));
            }
            return Ok(BaseResult.Result(null, "操作成功！", "200", 0));
        }    



    }
}
